home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TPTUTR0F.ZIP / PASCAL14.TXT < prev    next >
Text File  |  1996-02-03  |  13KB  |  350 lines

  1.                        Turbo Pascal for DOS Tutorial
  2.                            by Glenn Grotzinger
  3.                           Part 14: The CRT unit.
  4.                 copyright (c) 1995-96 by Glenn Grotzinger
  5.  
  6. Hello.  Here is a solution from last time...
  7.  
  8. {$M 65520,0,655360}
  9. {$B+}
  10. program part13; uses dos;
  11.  
  12.   var
  13.     drive, s: string;
  14.     drivesize: longint;
  15.     thefile: text;
  16.  
  17.   function number(n: longint): string; forward;
  18.  
  19.   procedure listdirsizes(filedir: string);
  20.     var
  21.       size: longint;
  22.       f: string;
  23.       dirinfo: searchrec;
  24.     begin
  25.       size := 0;
  26.       f := filedir + '\*.*';
  27.       findfirst(f, $27, dirinfo); { allfiles - VolumeID - directory }
  28.       while doserror = 0 do
  29.         begin
  30.           size := size + dirinfo.size;
  31.           findnext(dirinfo);
  32.         end;
  33.       writeln(thefile, filedir, number(size):50-length(filedir));
  34.       drivesize := drivesize + size;
  35.       findfirst(f, $10, dirinfo);
  36.       while doserror = 0 do
  37.         begin
  38.          { for some reason, the findfirst above doesn't work right. }
  39.          { Unable to determine exactly why...:<                     }
  40.           if (dirinfo.attr = $10) and (dirinfo.name <> '.')
  41.              and (dirinfo.name <> '..') then
  42.             begin
  43.               s := filedir + '\' + dirinfo.name;
  44.               listdirsizes(s);
  45.             end;
  46.           findnext(dirinfo);
  47.         end;
  48.     end;
  49.  
  50.   function number;
  51.     var
  52.       i, j: integer;
  53.       s, r: string;
  54.       m: integer;
  55.     begin
  56.       str(n, s);
  57.       r := '';
  58.       i := length(s);
  59.       if i > 3 then
  60.         begin
  61.           while i > 3 do
  62.             begin
  63.               for j := i downto i-2 do
  64.                 r := s[j] + r;
  65.               r := ',' + r;
  66.               i := i - 3;
  67.             end;
  68.           for j := i downto 1 do
  69.             r := s[j] + r;
  70.           number := r;
  71.         end
  72.       else
  73.         number := s;
  74.     end;
  75.  
  76.   procedure writehelp;
  77.     begin
  78.       writeln('Include a drive like this: c:');
  79.       halt(1);
  80.     end;
  81.  
  82.   begin
  83.     assign(thefile, 'FILESLST.TXT');
  84.     rewrite(thefile);
  85.     if paramcount <> 1 then
  86.       writehelp;
  87.     drive := paramstr(1);
  88.     if (length(drive) <> 2) or (drive[2] <> ':') then
  89.       writehelp;
  90.     drive[1] := upcase(drive[1]);
  91.     writeln(thefile, 'Drive: ', drive[1]);
  92.     listdirsizes(drive);
  93.     writeln(thefile, '===============':50);
  94.     writeln(thefile, number(drivesize):50);
  95.     close(thefile);
  96.   end.
  97.  
  98. In this part, we will try to discuss the complete usage of the CRT unit.
  99. The CRT unit will allow with text different things, such as PC speaker
  100. control, control of the screen for screen clears, positioning of the
  101. cursor, changing the color of text and background, and so forth.
  102.  
  103. Basically, when the uses crt, or wincrt, is used, direct writes to the
  104. screen are initiated, and can not be redirected to text file.
  105.  
  106. Boolean variables.
  107. ==================
  108. There is a list of boolean control variables accessible through CRT.
  109.  
  110. CheckBreak.  It is true by default. If it's false, CTRL+BREAK will be
  111. unfunctional.
  112.  
  113. CheckEOF.  It is true by default.  If it's false, CTRL+Z when pressed
  114. will not represent an EOF in the program.
  115.  
  116. CheckSnow.  Enables and disables "snow checking" on CGA adapters.
  117.  
  118. DirectVideo.  Enables and disables direct video writes.
  119.  
  120. Keypressed.  Will return true if a key has been pressed.  Usable in a
  121. loop for doing something until the user stops it.  A function like this
  122. is similarly used for example in screen savers to stop the display.
  123. Here is a small example to see how it acts.
  124.  
  125. program the_key_pressed; uses crt;
  126.   begin
  127.     repeat
  128.       write(#254);
  129.     until keypressed;
  130.   end.
  131.  
  132. This program should write ascii character #254 to the screen repeatedly
  133. until the user presses a key.
  134.  
  135. Screen Manipulation Commands
  136. ============================
  137. Clreol;  Clears to the end of the line for the currently active window
  138. from the current cursor position.  The concept of "currently active
  139. window" will be explained later.
  140.  
  141. Clrscr;  Clears the screen.  Already used and demonstrated.
  142.  
  143. Delline;  Deletes the current line containing the cursor.
  144.  
  145. Insline;  Inserts line at current position.
  146.  
  147. PC Speaker Sounds and Machine Delay
  148. ===================================
  149. These are the basics of controlling the PC Speaker to make sounds.
  150. The first thing is that there is an ASCII character #7 which will cause
  151. a standard DOS PC Speaker beep.
  152.  
  153. Then there are some pascal procedures which will offer much more control.
  154. You will have to experiment with it using different numbers in the delay
  155. and sound value.
  156.  
  157. Delay(milliseconds: word); will delay the program for approximately the
  158. number time in Milliseconds.
  159. Sound(hertz: word); will start up the PC speaker at a specified frequency.
  160. NoSound; will stop the PC speaker.
  161.  
  162. Here is an example of use of the PC speaker through TP.
  163.  
  164. program test_of_pc_speaker; uses crt;
  165.   begin
  166.     sound(1500);
  167.     delay(500);
  168.     nosound;
  169.   end.
  170. Basically, we're emitting a sound of 1500 Hz, for a time of .5 seconds.
  171.  
  172. Note: The delay procedure can be used anytime to "freeze" up the system
  173. for a specified period of time, say if we want to delay between writes.   
  174.  
  175. Screen Positioning
  176. ==================
  177. These is a command to control positioning of writing on the screen.
  178.  
  179. gotoxy(x, y); is a procedure that will position the cursor at the point
  180. defined by x, y.  The standard defintion of the screen positioning is
  181. a cartesian system like this for the standard video mode (on most systems
  182. when you boot them up).
  183.  
  184. (1, 1)(2,1)----------------------------------------------...  (x, 1)
  185. (1, 2)
  186.   |
  187. (1, 4)
  188.   |
  189.   |
  190.   |
  191.   |
  192.   |
  193.   |
  194.  ...
  195. (1, y)
  196.  
  197. x and y is defined by the current video mode, which is settable.  We will
  198. discuss how to set the current video mode.   The standard video mode at
  199. bootup is 80x25.
  200.  
  201. wherex is the current x position of the cursor, while wherey is the current
  202. position of the y variable.  gotoxy(wherex, wherey+1) is valid as an
  203. example, and will move the cursor DIRECTLY down one unit, irreguardless
  204. of where it is (as long as x and y are valid, most of these commands are
  205. inactive if they get bad data)
  206.  
  207. The view window can also actually be changed.  The window function can
  208. be used for this purpose.  The window is measured by the coordinate of
  209. the upper left hand corner of the window (x1, y1), and the lower right
  210. hand corner of the window(x2, y2).  The syntax if we call window is
  211. window(x1,y1,x2,y2);  This is one procedure that you really need to
  212. experiment with to see its effects.  As a note, most, if not all of the
  213. screen writing commands are affected by this window...
  214.  
  215. Changing Text Color, Appearance, and Mode
  216. =========================================
  217. These are functions and procedures which control the appearance of text.
  218.  
  219. HighVideo; will select high intensity characters.
  220. LowVideo; will select low intensity characters.
  221. NormVideo; will select the original intensity at startup.
  222.  
  223. Textcolor(color); and Textbackground(color); are used to control the
  224. foreground and background color respectively.  Foreground colors
  225. work from 0-15 and the background colors are from 0-8.  Blinking
  226. foreground text adds 128 to the value.  A list of text color constants
  227. which are equated with these numbers can be found on page 192 of the
  228. TP programmer's reference.
  229.  
  230. LastMode is a word variable which will hold the current video mode that
  231. the system was in when the program started execution.
  232.  
  233. Changing text mode actually involves changing what x and y as was originally
  234. stated in the description of gotoxy.  textmode(modeconstant); actually
  235. does this.  As with everything in this part, you should experiment with
  236. all of the commands and procedures defined, because you can't exactly
  237. be showed the effects of these commands in this tutorial.  A list of
  238. mode constants can be found on page 26 of the TP programmer's reference.
  239.  
  240. Here is a sample program which demonstrates several of the functions and
  241. procedures listed, and gives us an idea of what can be done with
  242. manipulation of text windows, colors, and so forth.  Be sure to run this
  243. one, and experiment with changing some of the values and actions to see
  244. what happens.
  245.  
  246. program demo_of_fun_text_manipulation; uses crt;
  247.   var
  248.     oldx, oldy: byte;
  249.   begin
  250.     { save original position of cursor so we can return there later }
  251.     oldx := wherex;
  252.     oldy := wherey;
  253.  
  254.     window(2,2,20,20);
  255.     textbackground(Blue);
  256.     clrscr; { blue background now b/c blue when clrscr was called }
  257.     highvideo;
  258.     window(10,10,15,15);
  259.     textbackground(Green);
  260.     clrscr;
  261.     textcolor(LightCyan + blink);
  262.     write('Hello, how are we doing today?');
  263.     window(1,1,80,25);
  264.     gotoxy(oldx, oldy);
  265.   end.
  266. Reading Characters from the Keyboard
  267. ====================================
  268. You may have been wondering, how to directly read things from the keyboard.
  269. The CRT offers a function called readkey, which is a parameterless function
  270. that returns a character.  Unlike read or readln, it does not require a
  271. CR to initiate.  You may see this by replacing a character read from the
  272. keyboard somewhere in your program with something like this:
  273.  
  274.                 answer := readkey;
  275.  
  276. You will notice that you did not have to press ENTER to move on.  Also,
  277. you will notice that the character didn't echo.  Very useful for password
  278. applications.
  279.  
  280. The next advantage of readkey is that it can be used to read about every
  281. key or key combination on the keyboard.  For this tutorial, we will discuss
  282. simply the keys that can be accessed via one keypress using a basic standard
  283. to be discussed in the next paragraph.
  284.  
  285. Extended keys, like the function keys, or the arrow keypad may be read
  286. using readkey.  These keys generate a #0 character as their first character
  287. followed by an extended scan code.  These vary depending upon which key
  288. is depressed.  To read extended keys, we would have to use a structure
  289. such as this:
  290.  
  291.             char := readkey;
  292.             if char = #0 then
  293.               char := readkey;
  294.  
  295. Most of the one press keys on the keyboard may be detected using this
  296. method.  Examples of those keys which can't be detected using the
  297. method described by the snippet of code above are the shift keys,
  298. capslock, ScrollLock, NumLock, F11, F12, the CTRL keys, and the ALT
  299. keys.  There are other methods available for those keys and key comb-
  300. inations.  I recommend as practice that you set up a reader program that
  301. will tell you what the extended character codes are for each of the
  302. extended keys (any key not on the basic keyboard).
  303.  
  304. Practice Programming Problem #14
  305. ================================
  306. Create a program in Pascal and entirely Pascal that will do the following:
  307.  
  308. 1) Using normal text mode, set up a screen that has a green background
  309. all the way around the screen as a frame of 4 units in size all the way
  310. around.  Inside of that, place a red frame of 1 unit all the way around.
  311.  
  312. 2) Inside all of the frames described in part 1, set up a title that
  313. states "Keypress Detector" on the first line available below the red
  314. frame, in an high-intensity light cyan.  Be sure this title is NEVER
  315. overwritten by scrolling text.
  316.  
  317. 3) The "Keypress Detector" title should be on a black background.  Also,
  318. the text which shows what keys we have pressed are should be a dull
  319. lightblue on a black background.  The exception to the last statment is
  320. that any standard key identity in the statement should be in a bright
  321. green, and any extended key identity in the statment should be in a
  322. bright red.  Key identity will be evident as I give a sample.
  323.  
  324. (properly inside the "frame")
  325.  
  326. Keypress Detector (press ESC 5 times to terminate)
  327. You pressed the F1 key.
  328. You pressed the SPACE key.
  329. You pressed the LEFT key.
  330. You pressed the Backspace key.
  331.  
  332. Note: Some of the keys on the keyboard are singular control characters.
  333. Look at your ASCII chart.  For example, the TAB key is actually ASCII
  334. character #9, and character #9 should be recognized as such...
  335.  
  336. F1, SPACE, LEFT, and BACKSPACE are the key identities as I described
  337. earlier...
  338.  
  339. 4) Make sure that in order to terminate the program, the ESC key gets
  340. pressed 5 times, not consecutively (be sure to indicate the ESC key has
  341. been pressed each time).  Upon termination, the entire screen should
  342. visibly scroll up until none of what we wrote is visible anymore (clue:
  343. use delay).
  344.  
  345. Next Time
  346. =========
  347. We will discuss the usage of the QuickSort and ShellSort algorithms.
  348. Please by all means send comments to ggrotz@2sprint.net.
  349.  
  350.